﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;


namespace IFServerDLL.Model
{
    public class TradeGroupBase
    {
        public TradeGroupBase()
        {

        }
        private string _Instrument;
        public string Instrument
        {
            get { return _Instrument; }
        }
        private decimal _OpenAvgPrice;
        public decimal OpenAvgPrice
        {
            get { return _OpenAvgPrice; }
        }
        private decimal _CloseAvgPrice;
        public decimal CloseAvgPrice
        {
            get { return _CloseAvgPrice; }
        }
        private int _SumVolume;
        public int SumVolume
        {
            get { return _SumVolume; }
        }
        private decimal _SumCommission;
        public decimal SumCommission
        {
            get { return _SumCommission; }
        }


        private MyEnumDirection _BuyOrSell;
        public MyEnumDirection BuyOrSell
        {
            get { return _BuyOrSell; }
        }
        private decimal _SumCloseProfit;
        public decimal SumCloseProfit
        {
            get { return _SumCloseProfit; }
        }
        private List<TradeOnHand> _ListOnhandHistory = new List<TradeOnHand>();
        public List<TradeOnHand> ListOnhandHistory
        {
            get { return _ListOnhandHistory; }
        }

        public void AddTradeOnhand(TradeOnHand onhand)
        {
            
            if (onhand.SumALLVolume != 0||onhand.BuyOrSell!=_BuyOrSell)
            {
                throw new Exception("unable to add tradeonhand");
            }
            _ListOnhandHistory.Add(onhand);

            _CloseAvgPrice = _ListOnhandHistory.Average(p=>p.AvgClosePrice);
            _OpenAvgPrice = _ListOnhandHistory.Average(p => p.AvgOpenPrice) ;
            _SumCloseProfit = _ListOnhandHistory.Sum(p => p.CloseProfit) ;
            _SumCommission = _ListOnhandHistory.Sum(p => p.SumCommission) ;
            int volume = 0;
            foreach (var x in _ListOnhandHistory)
            {
                volume += x.ListTrade.Where(p => p.ALLVolumne < 0).Sum(p => p.ALLVolumne);
            }
            _SumVolume =Math.Abs( volume);
        }
        public TradeGroupBase(TradeOnHand onhand)
        {
            _BuyOrSell = onhand.BuyOrSell;
            
            _Instrument = onhand.Instrument;
            _ListOnhandHistory.Add(onhand);
            _CloseAvgPrice = onhand.AvgClosePrice;
            _OpenAvgPrice = onhand.AvgOpenPrice;
            _SumCloseProfit = onhand.CloseProfit;
            _SumCommission = onhand.SumCommission;
            _SumVolume = onhand.SumALLVolume;
        }
    }
}
